package com.qying.metrics;

import com.codahale.metrics.*;
import org.springframework.boot.autoconfigure.batch.BatchProperties;

import java.util.PriorityQueue;
import java.util.Queue;
import java.util.concurrent.TimeUnit;

public class MetricsTest {

    static final MetricRegistry metrics = new MetricRegistry();

    public static void main(String[] args) {
        startReport();
        Meter requests = metrics.meter("requests");
        requests.mark();
        wait5Seconds();
    }

    static void startReport() {
        ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics)
                .convertRatesTo(TimeUnit.SECONDS)
                .convertDurationsTo(TimeUnit.MILLISECONDS)
                .build();
        reporter.start(1, TimeUnit.SECONDS);
    }

    static void wait5Seconds() {
        try {
            Thread.sleep(5 * 1000);
        }catch (InterruptedException e){}
    }

    static Counter pendingJobs = metrics.counter(MetricRegistry.name(
            QueueManager.class, "pending-jobs"));

    static void addJob(BatchProperties.Job job){
        pendingJobs.inc();

    }

}


class QueueManager {
    private final Queue queue;

    public QueueManager(MetricRegistry metrics, String name) {

        this.queue = new PriorityQueue();
        metrics.register(MetricRegistry.name(QueueManager.class, name, "size"),
                new Gauge<Integer>() {
                    @Override
                    public Integer getValue() {
                        return queue.size();
                    }
                });
    }


}